package com.yx.backstage.reptile;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.backstage.model.BookMessage;
import com.yx.backstage.model.ChapterMessage;
import com.yx.backstage.service.BookMessageService;
import com.yx.backstage.service.ChapterMessageService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;

/**
 * @author YangXiong
 */
@Component
@Slf4j
public class GetBookMessage {
    private static final int TIME_OUT = 10000;
    @Resource
    private BookMessageService bookMessageService;
    @Resource
    private ChapterMessageService chapterMessageService;

    public void getBookMessage() throws Exception {
        int flag = 0;
        int i = 1;
        String baseUrl = "http://www.siluwx.org";
        Document index = getDocument(baseUrl);
        Element body = index.body();
        Elements item = body.getElementsByClass("item");
        log.info("========解析开始=================请等待！！！！");
        for (Element element : item) {
            BookMessage bookMessage = new BookMessage();
            String name = element.select("a").text();
            if (name.startsWith("诸天皇帝") || name.startsWith("对不起")) {
                //这两本书有问题，舍弃不要
                continue;
            }
            bookMessage.setName(name);
            log.info("======>开始抓取第{}本书{}=======", flag + i, name);
            String img = element.select("img").attr("src");
            bookMessage.setUrl(baseUrl + img);
            String href = element.select("a").attr("href");
            String detail = baseUrl + href;
            Element detailBody = getDocument(detail).body();
            Elements bookMes = detailBody.getElementsByClass("bookdmore").select("p");
            for (Element bookMe : bookMes) {
                String label = bookMe.text();
                handlerLabel(label, bookMessage);
            }
            Elements bookText = detailBody.getElementsByClass("bookdtext");
            String introduction = bookText.select("p").get(0).text();
            bookMessage.setIntroduction(introduction);

            QueryWrapper<BookMessage> checkQueryWrapper = new QueryWrapper<>();
            checkQueryWrapper.eq("name", name);
            BookMessage check = bookMessageService.getOne(checkQueryWrapper);
            if (Objects.isNull(check)) {
                boolean save = bookMessageService.save(bookMessage);
                if (!save) {
                    continue;
                }
            } else {
                i++;
                log.info(name + "=========已经存在！！！");
                continue;
            }
            LambdaQueryWrapper<BookMessage> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BookMessage::getName, name);
            queryWrapper.eq(BookMessage::getAuthor, bookMessage.getAuthor());
            BookMessage bookMessageDO = bookMessageService.getOne(queryWrapper);

            String attr = bookText.select("a").get(2).attr("href");
            String chapterUrl = baseUrl + attr;
            Element chapterBody = getDocument((chapterUrl)).body();
            Elements a = chapterBody.getElementById("list").select("a");
            for (Element one : a) {
                ChapterMessage chapterMessage = new ChapterMessage();
                String text = one.text();
                chapterMessage.setChapterName(text);
                chapterMessage.setBookId(bookMessageDO.getId());
                String chapUrl = baseUrl + one.attr("href");
                Element chapBody = getDocument(chapUrl).body();
                Element elementById = chapBody.getElementById("content");
                String content = elementById.child(1).text();
                chapterMessage.setChapterContext(content);

                QueryWrapper<ChapterMessage> messageDOQueryWrapper = new QueryWrapper<>();
                messageDOQueryWrapper.eq("chapter_name", text);
                ChapterMessage messageDO = chapterMessageService.getOne(messageDOQueryWrapper);
                if (Objects.nonNull(messageDO)) {
                    continue;
                }
                chapterMessageService.save(chapterMessage);
            }
            flag++;
            log.info("======>抓取第{}本书{}完成===============", flag, name);
            if (flag >= 10) {
                log.info("此次抓取书本10本，抓取结束");
                System.exit(1);
            }
        }

    }

    private Document getDocument(String baseUrl) throws IOException {
        Document index = Jsoup.parse(new URL(baseUrl), TIME_OUT);
        if (Objects.isNull(index)) {
            getDocument(baseUrl);
        }
        return index;
    }

    private void handlerLabel(String text, BookMessage bookMessage) {
        if (text.contains("分类")) {
            bookMessage.setKind(text.substring(3));
        }
        if (text.contains("类型")) {
            bookMessage.setType(text.substring(3));
        }
        if (text.contains("作者")) {
            bookMessage.setAuthor(text.substring(3));
        }
        if (text.contains("状态")) {
            bookMessage.setStatus(text.substring(3));
        }
        if (text.contains("大小")) {
            bookMessage.setSize(text.substring(3));
        }
        if (text.contains("最新章节")) {
            bookMessage.setLastChapter(text.substring(5));
        }
        if (text.contains("最新时间")) {
            String time = text.substring(5);
            bookMessage.setLastUpdateTime(DateUtil.parse(time));
        }
    }
}
